package www.dianbaozu.com.ui.my

import android.annotation.SuppressLint
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentCancellationBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.extension.nav
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description: 账号注销提醒
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class CancellationFragment : BaseLoadingFragment<FragmentCancellationBinding>(){

    private val mBaseApiViewModel: ContractViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(requireActivity(),"确认注销重要提醒", action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_cancellation

    override fun lazyInitView() {
        webSetting()
        launchRequest()
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun webSetting() {
        val settings: WebSettings = dataBinding.webView.settings
        settings.javaScriptEnabled = true
        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        settings.setSupportZoom(false)
        settings.builtInZoomControls = false
        settings.blockNetworkImage = true
        settings.allowFileAccess = false
        settings.domStorageEnabled = true
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        dataBinding.webView.webViewClient = WebViewClient()
        dataBinding.webView.webChromeClient = WebChromeClient()
//        dataBinding.webView.setBackgroundColor(getColorAgency(R.color.color000000))
    }

    private fun launchRequest() {
        mBaseApiViewModel.requestProtocol("注销提醒"){
            if(it.content.isNotEmpty()){
                dataBinding.tvLogoutReminder.text = it.content[0].content
                dataBinding.webView.loadDataWithBaseURL(null, it.content[0].content, "text/html" , "utf-8", null)
            }
        }
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.tvGiveUp -> actionLeft()
            R.id.tvCancellation -> destroy()
        }
    }

    private fun destroy() {
        CreateXPopupHelper.createShowStandardAlertPopupView(PopupViewEntity().apply {
            context = requireActivity()
            standardAlertPopupViewTitle = getString(R.string.com_delete_account)
            isShow = true
            standardAlertPopupViewCancelContent = "取消"
            standardAlertPopupViewConfirmContent = "确认"
            standardAlertPopupViewContent = "确定要注销账户吗? 该操作无法撤销！"
            confirmFunction = {
                mBaseApiViewModel.requestUniversally(
                        viewModelScope = lifecycleScope,
                        tipSuccess = "",
                        isShowToastTip = false,
                        request = { appDataManage -> appDataManage.requestRemove() }
                ) {
                    NIMHelper.logout()
                    SpUtil.clearAll()
                    nav(R.id.action_cancellationFragment_to_cancelledFragment)
                }
            }
        })
    }

}



